/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.woundedwolf.android.tumbleweed.examples;

import com.woundedwolf.android.tumbleweed.Engine;
import com.woundedwolf.android.tumbleweed.AbstractGame;
import com.woundedwolf.android.tumbleweed.Polygon;
import com.woundedwolf.android.tumbleweed.renderables.Polygons;
import com.woundedwolf.android.tumbleweed.elements.ACTION_TYPE;
import com.woundedwolf.android.tumbleweed.elements.ActionVector;
import com.woundedwolf.android.tumbleweed.elements.Vector2D;
import java.util.ArrayList;
import java.util.Random;

/**
 *
 * @author zagnut
 */
public class FallingSquares extends AbstractGame
{
    private ArrayList<Vector2D> points;
    private Polygons squares;
    private Random randomGenerator;
    private ActionVector action;

    public FallingSquares(Engine engine)
    {
        super(engine);
        points = new ArrayList<Vector2D>();
        squares = new Polygons();
        randomGenerator = new Random(2392);
        addRenderable(squares);
    }

    protected Polygon PolySquare(Vector2D center)
    {
        ArrayList<Vector2D> p = new ArrayList<Vector2D>();
        int size = randomGenerator.nextInt(55) + 5;
        p.add(new Vector2D(center.getXpos() - size / 2, center.getYpos() - size / 2));
        p.add(new Vector2D(center.getXpos() + size / 2, center.getYpos() - size / 2));
        p.add(new Vector2D(center.getXpos() + size / 2, center.getYpos() + size / 2));
        p.add(new Vector2D(center.getXpos() - size / 2, center.getYpos() + size / 2));
        p.add(new Vector2D(center.getXpos() - size / 2, center.getYpos() - size / 2));
        return new Polygon(0, 100, p);
    }

    public void updateGame()
    {
        action = dequeueAction();
        if (action != null)
        {
            if (action.getActionType() == ACTION_TYPE.ACTIN_DOWN)
            {
                Vector2D p = new Vector2D(action.getXpos(), action.getYpos());
                squares.addPolygon(PolySquare(p));
            }
        }

        for (Polygon square : squares.getPolygons())
        {
            for (Vector2D point : square.getPoints())
            {
                point.setYpos(point.getYpos() + 0.4f);
            }
        }
    }
}
